//
// Created by jiangpenghui on 2023/4/12.
//

#ifndef CMAKECXX_ARRAY_3_H
#define CMAKECXX_ARRAY_3_H

#include <cstddef>
#include <iostream>

using namespace std;

// 模板类型的模板参数
template<class T>
class Array_3 {
    enum {
        INIT = 10
    };

    T *data;
    size_t capactiy;
    size_t count;
public:
    // 构造函数赋值 data = new T[INIY)   capactiy = INIT
    Array_3() : count(0), data(new T[capactiy = INIT]) {
        cout << "INIT :" << INIT << "Date :" << data << endl;
    }

    ~Array_3() {
        delete[] data;
        cout << "Array Delete " << endl;
    }

    // 添加数组
    void push_back(const T &t) {
        if (count == capactiy) { // 容量已满
            size_t newCap = 2 * capactiy;
            T *newData = new T[newCap];
            // 转存数组
            std::copy(std::begin(data), std::end(data), std::begin(newData));

            delete[] data;
            data = newData;
            capactiy = newCap;
        }
        data[count++] = t;
    }

    T &pop_back() {
        //
        if (count > 0) {
            --count;
            T item = data[count];
            data[count] = nullptr;
            return item;
        } else {
            return nullptr;
        }
    }

    T *begin() { return data; }

    T *end() {
        // 指针相加
        return data + count;
    }
};

// 模板类型的模板参数(模板中使用其他模板）  class Seq 为Seq泛元

// Container<int,Array> container ;  声明改类
template<class T, template<class> class Seq>
class Container {
    Seq<T> seq;
public:
    void append(const T &t) {
        seq.push_back(t);
    }

    T *begin() {
        return seq.begin();
    }

    T *end() {
        return seq.end();
    }

};



#endif //CMAKECXX_ARRAY_3_H
